<?php

/*
 +-----------------------------------------------------------------------+
 | plugins/sieverules/save_sieverule.inc                                 |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
 |	 Provide functionality for user to save their sieve rules            |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 | Modifications by: Philip Weir                                         |
 +-----------------------------------------------------------------------+
 | Requires: plugins/sauserprefs/sieverules_func.inc                     |
 +-----------------------------------------------------------------------+

 $Id: $

*/

require_once('plugins/sieverules/sieverules_func.inc');

$name = trim(get_input_value('_name', RCUBE_INPUT_POST));
$fid = trim(get_input_value('_iid', RCUBE_INPUT_POST));
$join = trim(get_input_value('_join', RCUBE_INPUT_POST));
$disabled = trim(get_input_value('_disable', RCUBE_INPUT_POST));

$tests = $_POST['_test'];
$headers = $_POST['_header'];
$ops = $_POST['_operator'];
$sizeops = $_POST['_size_operator'];
$targets = $_POST['_target'];
$sizeunits = $_POST['_units'];
$comparators = $_POST['_comparator'];
$advops = $_POST['_advoperator'];
$advtargets = $_POST['_advtarget'];
$actions = $_POST['_act'];
$folders = $_POST['_folder'];
$addresses = $_POST['_redirect'];
$rejects = $_POST['_reject'];
$vacfroms = $_POST['_vacfrom'];
$vactos = $_POST['_vacto'];
$days = $_POST['_day'];
$handles = $_POST['_handle'];
$subjects = $_POST['_subject'];
$origsubjects = $_POST['_orig_subject'];
$msgs = $_POST['_msg'];
$charsets = $_POST['_charset'];
$flags = $_POST['_imapflags'];
$nfroms = $_POST['_nfrom'];
$nimpts = $_POST['_nimpt'];
$nmethods = $_POST['_nmethod'];
$noptions = $_POST['_noption'];
$nmsgs = $_POST['_nmsg'];

$script = array();
$script['join'] = ($join == 'allof') ? true : false;
$script['name'] = $name;
$script['disabled'] = $disabled;
$script['tests'] = array();
$script['actions'] = array();

// rules
$i = 0;
if ($join == 'any') {
	$script['tests'][0]['type'] = 'true';
}
else foreach($tests as $idx => $type) {
	// ignore the first (default) row
	if ($idx == 0)
		continue;

	$header = strip_val($headers[$idx]);
	$op = strip_val($ops[$idx]);
	$advop = strip_val($advops[$idx]);
	$target = strip_val($targets[$idx]);
	$advtarget = strip_val($advtargets[$idx]);
	$comparator = strip_val($comparators[$idx]);

	switch ($type) {
		case 'size':
			$sizeop = strip_val($sizeops[$idx]);
			$sizeunit = strip_val($sizeunits[$idx]);

			$script['tests'][$i]['type'] = 'size';
			$script['tests'][$i]['operator'] = $sizeop;
			$script['tests'][$i]['target'] = $target.$sizeunit;
			break;
		case 'exists':
		case 'header':
		case 'address':
		case 'envelope':
			if(preg_match('/^not/', $op) || preg_match('/^not/', $advop))
				$script['tests'][$i]['not'] = true;

			$op = preg_replace('/^not/', '', $op);
			$advop = preg_replace('/^not/', '', $advop);

			if ($op == 'exists') {
				$script['tests'][$i]['type'] = 'exists';
				$script['tests'][$i]['header'] = $header;
			}
			elseif ($op == 'advoptions') {
				$script['tests'][$i]['type'] = $type;
				$script['tests'][$i]['operator'] = $advop;
				$script['tests'][$i]['header'] = $header;
				$script['tests'][$i]['target'] = $advtarget;

				if (substr($advop, 0, 5) == 'count' || substr($advop, 0, 5) == 'value')
					$script['tests'][$i]['comparator'] = $comparator;
			}
			else {
				$script['tests'][$i]['type'] = $type;
				$script['tests'][$i]['operator'] = $op;
				$script['tests'][$i]['header'] = $header;
				$script['tests'][$i]['target'] = $target;
			}
			break;
	}
	$i++;
}

// actions
$RCMAIL->imap_init(TRUE);
$i = 0;
foreach($actions as $idx => $type) {
	// ignore the first (default) row
	if ($idx == 0)
		continue;

	$type = strip_val($type);

	$script['actions'][$i]['type'] = $type;

	switch ($type) {
		case 'fileinto':
			$folder = strip_val($folders[$idx]);
			$script['actions'][$i]['target'] = mod_mailbox($folder);
			break;
		case 'redirect':
			$address = strip_val($addresses[$idx]);
			$script['actions'][$i]['target'] = $address;
			break;
		case 'reject':
			$rejects = strip_val($rejects[$idx]);
			$script['actions'][$i]['target'] = $rejects;
			break;
		case 'vacation':
			$from = strip_val($vacfroms[$idx]);
			$to = strip_val($vactos[$idx]);
			$day = strip_val($days[$idx]);
			$handle = strip_val($handles[$idx]);
			$subject = strip_val($subjects[$idx]);
			$origsubject = strip_val($origsubjects[$idx]);
			$msg = strip_val($msgs[$idx]);
			$charset = strip_val($charsets[$idx]);
			$script['actions'][$i]['days'] = $day;
			$script['actions'][$i]['from'] = $from;
			$script['actions'][$i]['addresses'] = $to;
			$script['actions'][$i]['handle'] = $handle;
			$script['actions'][$i]['subject'] = $subject;
			$script['actions'][$i]['origsubject'] = $origsubject;
			$script['actions'][$i]['msg'] = $msg;
			$script['actions'][$i]['charset'] = $charset;
			break;
		case 'imapflags':
			$flag = strip_val($flags[$idx]);
			$script['actions'][$i]['target'] = $flag;
			break;
		case 'notify':
			$from = strip_val($nfroms[$idx]);
			$importance = strip_val($nimpts[$idx]);
			$method = strip_val($nmethods[$idx]);
			$option = strip_val($noptions[$idx]);
			$msg = strip_val($nmsgs[$idx]);
			$script['actions'][$i]['from'] = $from;
			$script['actions'][$i]['importance'] = $importance;
			$script['actions'][$i]['method'] = $method;
			$script['actions'][$i]['options'] = $option;
			$script['actions'][$i]['msg'] = $msg;
			break;
    }

    $i++;
}

sieverules_init();

if (!isset($SCRIPT[$fid]))
	$fid = $SIEVE->script->add_rule($script);
else
	$fid = $SIEVE->script->update_rule($fid, $script);

if ($fid !== false)
	$save = $SIEVE->save();

if ($save && $fid !== false)
	$OUTPUT->show_message('filtersaved', 'confirmation');
else
	$OUTPUT->show_message('filtersaveerror', 'error');

// go to next step
rcmail_overwrite_action('edit-sieverule');

function strip_val($str) {
	return trim(htmlspecialchars_decode($str));
}

function LogMe($str){
	//Start to build whitelist / blacklist
	$myFile = "/home/undertoe/webmail/logs/ajax.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	$stringData = $str . "\n\n";
	fwrite($fh, $stringData);
	fclose($fh);
}
?>